package com.ruoyi.project.weixin.mp.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.weixin.mp.entity.WxUser;
import com.ruoyi.project.weixin.mp.service.WxUserService;
import com.ruoyi.project.weixin.mp.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 用户表
 *
 * @author niefy
 * @email niefy@qq.com
 * @date 2020-03-07 13:55:23
 */
@RestController
@RequestMapping("/manage/wxUser")
@Api(tags = {"公众号粉丝-管理后台"})
public class WxUserManageController {

    @Autowired
    private WxUserService userService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('wx:wxuser:list')")
    @ApiOperation(value = "列表")
    public AjaxResult list(@RequestParam Map<String, Object> params) {
        PageUtils page = new PageUtils(userService.queryPage(params));
        return AjaxResult.success(page);
    }

    /**
     * 列表
     */
    @PostMapping("/listByIds")
    @PreAuthorize("@ss.hasPermi('wx:wxuser:list')")
    @ApiOperation(value = "列表-ID查询")
    public AjaxResult listByIds(@CookieValue String appid,@RequestBody String[] openids){
        List<WxUser> users = (List<WxUser>) userService.listByIds(Arrays.asList(openids));
        return AjaxResult.success(users);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{openid}")
    @PreAuthorize("@ss.hasPermi('wx:wxuser:info')")
    @ApiOperation(value = "详情")
    public AjaxResult info(@CookieValue String appid,@PathVariable("openid") String openid) {
        WxUser wxUser = userService.getById(openid);
        return AjaxResult.success(wxUser);
    }

    /**
     * 同步用户列表
     */
    @PostMapping("/syncWxUsers")
    @PreAuthorize("@ss.hasPermi('wx:wxuser:save')")
    @ApiOperation(value = "同步用户列表到数据库")
    public AjaxResult syncWxUsers(@RequestBody JSONObject jsonObject) {
        String appid = jsonObject.getString("appid");
        userService.syncWxUsers(appid);
        return AjaxResult.success("任务已建立");
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @PreAuthorize("@ss.hasPermi('wx:wxuser:delete')")
    @ApiOperation(value = "删除")
    public AjaxResult delete(@CookieValue String appid,@RequestBody String[] ids) {
        userService.removeByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }

}